using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Universum.Model.Services.Classes;
using Universum.Views.MyClasses;
using Universum.Model.Domain.Classes;
using Universum.Views.Classes;
using Universum.Model.Services.MyClasses;
using Universum.Model.Domain.MyClasses;
using Universum.Model.Services;
using Universum.Infrastructure.Configuration;

namespace Universum.Controllers
{
	
	public class MyClassesController : UniversumController
	{
		IClassesService _ClassesService;
		IPageArticleService _PageArticleService;
		ILabWorkArticleService _LabWorkArticleService;
		IPageGroupService _PageGroupService;
		ISubmissionService _SubmissionService;
		ISignupService _SignupService;
		public MyClassesController(
			System.Security.Principal.IPrincipal principal,
			IClassesService classesService,			
			ILabWorkArticleService labWorkArticleService,
			IPageArticleService pageArticleService,
			IPageGroupService pageGroupService,
			ISubmissionService submissionService,
			ISignupService signupService
			
			)
			: base(principal)
		{
			_ClassesService = classesService;
			_LabWorkArticleService = labWorkArticleService;
			_PageArticleService = pageArticleService;
			_PageGroupService = pageGroupService;
			_SubmissionService = submissionService;
			_SignupService = signupService;
		}
		//
		// GET: /MyClasses/
		[Authorize(Roles = ConstantIdentifiers.ROLE_OF_STUDENT)]
		public ActionResult Index()
		{
			var model = new MyClassesIndexViewModel();
			model.SignedClasses = _SignupService.SignedForClasses();
			model.UnSignedClasses = _ClassesService.GetAllClasses()
		.Where(it => model.SignedClasses.Any(el => el.ID == it.ID) == false)
		.ToList();
			ViewData.Model = model;
			return View();
		}
		[Authorize(Roles = ConstantIdentifiers.ROLE_OF_STUDENT)]
		public ActionResult Class(string id)
		{
			Class @class = _ClassesService.GetClassBySlug(id);
			var categories = _PageGroupService.GetPageGroupsByClassId(@class.ID);

			var rootNodes = categories.Where(page_group => page_group.ParentGroupId == Guid.Empty).ToList();
			var model = new MyClassesClassViewModel();
			model.Root = new PageGroupNode();
			model.Class = @class;
			model.Root.Nodes = (from node in categories
								where node.ParentGroupId == Guid.Empty
								orderby node.Order
								select new PageGroupNode
								{
									PageGroup = node,
									ArticleList = _PageArticleService.GetPublishedArticlesByPageGroupId(node.ID)

								}).ToList();

			FillRecursive(model.Root.Nodes.ToList(), categories);
			ViewData.Model = model;
			return View();
		}

		private void FillRecursive(List<PageGroupNode> list, IList<PageGroup> categories)
		{
			list.ForEach(
				item =>
				{
					item.Nodes = (from node in categories
								  where node.ParentGroupId == item.PageGroup.ID
								 orderby node.Order
								  select new
								  PageGroupNode
								  {
									  PageGroup = node,
									  ArticleList = _PageArticleService.GetPublishedArticlesByPageGroupId(node.ID)

								  }
								  ).ToList();
					FillRecursive(item.Nodes.ToList(), categories);
				});


		}
		[Authorize(Roles = ConstantIdentifiers.ROLE_OF_STUDENT)]
		public ActionResult Article(string id){

			var model = new MyClassesArticleViewModel
			{
				Article = _PageArticleService.GetArticleBySlug(id),

			};
			model.Class = _ClassesService.GetClassById(_PageGroupService.GetPageGroupById(model.Article.PageGroupId).ClassId);
			if (_SignupService.SignedForClasses().Contains(model.Class))
			{
				ViewData.Model = model;

				return View();
			}
			else
			{
				return RedirectToAction<MyClassesController>(it => it.Index());
			}
		}
		public ActionResult Lab(string id){
			var model = new MyClassesLabViewModel();
			
			model.Lab = _LabWorkArticleService.GetLabWorkBySlug(id);
			model.Submission = _SubmissionService.CreateNewSubmissionOrGetBy(model.Lab.ID, User.Identity.Name);
			model.Class = _ClassesService.GetClassById(_PageGroupService.GetPageGroupById(model.Lab.PageGroupId).ClassId);
			ViewData.Model = model;
				
				
			return View();
		}
		[AcceptVerbs( HttpVerbs.Post)]
		[Authorize(Roles = ConstantIdentifiers.ROLE_OF_STUDENT)]
		public ActionResult SignUp(Guid id/*class id*/)
		{

			_SignupService.SignUpFor(id);
			return RedirectToAction<MyClassesController>(c => c.Index());
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[Authorize(Roles = ConstantIdentifiers.ROLE_OF_STUDENT)]
		public ActionResult Submit(HttpPostedFileBase workFile, 
			LabWorkSubmission submission)
		{
			var lab = _LabWorkArticleService.GetLabWorkArticleById(submission.LabWorkArticleId);
			
			_SubmissionService.Register(submission,  workFile);

			return RedirectToAction<MyClassesController>(
				c => c.Lab(lab.Slug));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[Authorize(Roles = ConstantIdentifiers.ROLE_OF_TEACHER_OR_STUDENT)]
		public ActionResult Download(Guid id)
		{
			var submision = _SubmissionService.GetSubmisionById(id);
			if (submision != null)
			{
				var stream = _SubmissionService.GetFileSubmisionStream(submision);
				return
				new FileStreamResult
				(stream, submision.ContentType ?? ConstantIdentifiers.FILETYPE_APPLICATION_ZIP)
				{
					FileDownloadName = submision.FileName
				};
			}
		
			return RedirectToAction<MyClassesController>(
				c => c.Index());
		}
	}
}
